//
//  THPageControl.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 BaseBusiness. All rights reserved.
//

#import "THPageControl.h"

@interface THPageControl()

@property (strong,nonatomic) UIImageView * currentView;
@property (strong,nonatomic) NSMutableArray * normalViews;

@property (strong,nonatomic) UIImage * selectedImg;
@property (strong,nonatomic) UIImage * normalImg;
@property (nonatomic) CGSize dotSize;
@property (nonatomic) CGFloat space;

@end

@implementation THPageControl

+ (THPageControl *)getInstance
{
    return [[self alloc] initWithSelectedImg:[UIImage imageNamed:@""] normalImg:[UIImage imageNamed:@""] dotSize:CGSizeMake(KRelative(14),KRelative(14)) dotSpace:KRelative(34)];
}

- (id)initWithSelectedImg:(UIImage *)selectedImg normalImg:(UIImage *)normalImg dotSize:(CGSize)dotSize dotSpace:(CGFloat)space
{
    self = [super init];
    _selectedImg = selectedImg;
    _normalImg = normalImg;
    _space = space;
    _normalViews = [NSMutableArray array];
    if (CGSizeEqualToSize(CGSizeZero,dotSize))
    {
        dotSize = [_selectedImg size];
    }
    dotSize.height = ceilf(dotSize.height);
    dotSize.width = ceilf(dotSize.width);
    _dotSize = dotSize;
    [self createUI];
    return self;
}

- (void)createUI
{
    _currentView = [[UIImageView alloc] initWithImage:_selectedImg];
    _hidesForSinglePage = YES;
    self.hidden = YES;
    [self addSubview:_currentView];
}

- (void)updateConstraints
{
    [self mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@(self.numberOfPages * self.space));
    }];
    for (NSInteger index = 0; index < _normalViews.count; index++)
    {
        UIView * normalView = _normalViews[index];
        [normalView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.bottom.equalTo(@(0));
            make.centerX.equalTo(self.mas_left).offset(index * self.space + self.space / 2);
            make.width.equalTo(@(self.dotSize.width));
            make.height.equalTo(@(self.dotSize.height));
        }];
    }
    [self updateCurrentViewConstraints];
    [super updateConstraints];
}

- (void)updateCurrentViewConstraints
{
    @weakify(self);
    [_currentView mas_updateConstraints:^(MASConstraintMaker *make) {
        @strongify(self);
        make.top.bottom.equalTo(@(0));
        make.centerX.equalTo(self.mas_left).offset(self.currentPage * self.space + self.space / 2);
        make.width.equalTo(@(self.dotSize.width));
        make.height.equalTo(@(self.dotSize.height));
    }];
}

- (void)setNumberOfPages:(NSInteger)numberOfPages
{
    if (_numberOfPages != numberOfPages)
    {
        _numberOfPages = numberOfPages;
        [self createNormalViews];
        [self setNeedsUpdateConstraints];
        [self checkHidden];
    }
}

- (void)createNormalViews
{
    if (_normalViews.count < _numberOfPages)
    {
        NSInteger less = _numberOfPages - _normalViews.count;
        for (NSInteger index = 0; index < less; index++)
        {
            UIImageView * normalView = [[UIImageView alloc] initWithImage:_normalImg];
            [self addSubview:normalView];
            [_normalViews addObject:normalView];
        }
    }
    else
    {
        for (NSInteger index = _normalViews.count - 1; index >= _numberOfPages ; index--)
        {
            UIView * normalView = _normalViews[index];
            [normalView removeFromSuperview];
            [_normalViews removeObjectAtIndex:index];
        }
    }
    [self bringSubviewToFront:_currentView];
}

- (void)setCurrentPage:(NSInteger)currentPage
{
    currentPage = MIN(currentPage, _numberOfPages - 1);
    currentPage = MAX(0, currentPage);
    if (currentPage != _currentPage)
    {
        _currentPage = currentPage;
        [self updateCurrentViewConstraints];
    }
    for (UIView * view in _normalViews)
    {
        view.hidden = NO;
    }
    if (_normalViews.count > currentPage)
    {
        [_normalViews[currentPage] setHidden:YES];
    }
}

- (void)setHidesForSinglePage:(BOOL)hidesForSinglePage
{
    _hidesForSinglePage = hidesForSinglePage;
    [self checkHidden];
}

- (void)checkHidden
{
    self.hidden = (_hidesForSinglePage && _numberOfPages <= 1) || _numberOfPages == 0;
}

@end
